/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "route.h"
#include "cJSON.h"
#include "ctrl_common.h"
#include "storage.h"
#include "collector.h"
#include "dwlcd.h"

typedef struct
{
	cJSON *root;
  cJSON *params;
  cJSON *key;
}route_dk_down_static_type;
/* 本文件中调用的一些变量,为了减少栈的消耗,定义到函数外的全局变量,使用前手动清0 */
static route_dk_down_static_type dk_down_static;

uint8_t Route_dk_down_pt(const char *str, uint8_t len)
{
	int index;
	uint8_t err = 0;
  Ctrl_ParaDKType *dk = NULL;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&dk_down_static, 0, sizeof(route_dk_down_static_type));
	
  dk = (Ctrl_ParaDKType *)(proute->payload);
  
/* *** */ 	
  dk_down_static.root = cJSON_Parse(str);
  
  dk_down_static.params = cJSON_GetObjectItem(dk_down_static.root, "params");
	if(dk_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "zsb");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(dk_down_static.key->valuestring, NULL) != 0)
	{
		err = 3;
    goto fnout;
	}

#if (ROUTE_INTEGRATED > 0)
  /* 获取现有的参数 */
  *dk = Ctrl_ParaDK;
#endif
	
  
/************/
	dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dktd");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
  index = atoi(dk_down_static.key->valuestring);
  if((index != 0) && (index > DKCTR_PT_CHANNEL_ALLNUM))
  {
    err = 4;
    goto fnout;
  }
	
  index -= 1;
  
  dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dkkqtm1");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
  
  dk->dktm[index].dkkqtm1 = atoi(dk_down_static.key->valuestring);
  
  
  dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dkgbtm1");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 6;
    goto fnout;
  }
  
  dk->dktm[index].dkgbtm1 = atoi(dk_down_static.key->valuestring);
  
  
  dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dkkqtm2");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 7;
    goto fnout;
  }
  
  dk->dktm[index].dkkqtm2 = atoi(dk_down_static.key->valuestring);
  
  
  dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dkgbtm2");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 8;
    goto fnout;
  }
  
  dk->dktm[index].dkgbtm2 = atoi(dk_down_static.key->valuestring);
  
  //
  dk_down_static.key = cJSON_GetObjectItem(dk_down_static.params, "dkkq");
	if((dk_down_static.key == NULL) || (dk_down_static.key->type != cJSON_String))
  {
    err = 8;
    goto fnout;
  }
  
#if (ROUTE_INTEGRATED > 0)  
  if(dk_down_static.key->valuestring[0] == '1')
  {
    dk_act[index] = 1;
  }
  else if(dk_down_static.key->valuestring[0] == '0')
  {
    dk_act[index] = 0;
  }
  CtrDk_SetActuator(dk_act[index], index);
  Ctrl_RefreshStateDK(1);

	Storage_SaveType sftype;
	
	if(memcmp(&Ctrl_ParaDK, &dk, sizeof(Ctrl_ParaDKType)) != 0)
	{
		if(Ctr_CheckDKPara(dk) != 0)
		{
			err = 9;
			goto fnout;
		}
		
		Ctrl_ParaDK = *dk;
		
		sftype = SFLASH_SAVE_CTRDK;
		if(Storage_qhd != NULL)
		{
			xQueueSend(Storage_qhd, &sftype, 0);
		}
		
		Route_dj_tonet_ptpara();
	}
#else

#endif

fnout:
  cJSON_Delete(dk_down_static.key);
  cJSON_Delete(dk_down_static.params);
  cJSON_Delete(dk_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_dk_down_tg(const char *str, uint8_t len)
{

  return 0;
}
